package czhdao

import (
	"Cgo/front/models"
	"Cgo/global"
	"errors"
)

type hotelDao struct{}

var HotelDao = hotelDao{}

// 获取对应地区酒店信息
func (h hotelDao) GetHotelByRegion(region string) ([]models.CzhHotel, error) {
	var hotelList []models.CzhHotel
	if r := global.DB.Raw("SELECT * FROM czh_hotel WHERE region LIKE CONCAT('%', ?, '%')", region).Scan(&hotelList); r.RowsAffected > 0 {
		return hotelList, nil
	} else {
		return hotelList, errors.New("没有查到数据")
	}
}

// 根据id获取对应酒店信息
func (h hotelDao) GetHotelById(id string) (models.CzhHotel, error) {
	var hotel models.CzhHotel
	if r := global.DB.Raw("SELECT * FROM czh_hotel WHERE id = ?", id).Scan(&hotel); r.RowsAffected > 0 {
		return hotel, nil
	} else {
		return hotel, errors.New("没有查到数据")
	}
}

// 根据酒店id获取对应价格信息
func (h hotelDao) GetPriceByHoetlId(hotelId string) ([]models.CzhHotelPrice, error) {
	var priceList []models.CzhHotelPrice
	if r := global.DB.Raw("SELECT * FROM czh_hotel_price WHERE hotelid = ?", hotelId).Scan(&priceList); r.RowsAffected > 0 {
		return priceList, nil
	} else {
		return priceList, errors.New("没有查到数据")
	}
}

// 根据酒店id以及入住人数获取对应价格信息
func (h hotelDao) GetPriceByHoetlIdNum(hotelId string, num string) ([]models.CzhHotelPrice, error) {
	var priceList []models.CzhHotelPrice
	if r := global.DB.Raw("SELECT * FROM czh_hotel_price WHERE hotelid = ? AND num = ?", hotelId, num).Scan(&priceList); r.RowsAffected > 0 {
		return priceList, nil
	} else {
		return priceList, errors.New("没有查到数据")
	}
}

// 添加目的地订单
func (h hotelDao) AddHotelnOrder(name string, userid string, price string, username string, people string, coverimg string, room string, food string) error {
	if r := global.DB.Exec("INSERT INTO czh_hotel_orders (name, userid, price, username, people, coverimg, room, food) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", name, userid, price, username, people, coverimg, room, food); r.Error != nil {
		return r.Error
	} else {
		return nil
	}
}

// 根据用户id获取对应用户订单信息
func (h hotelDao) GetHotelOrderByUserId(userid string) ([]models.CzhHotelOrder, error) {
	var hotelOrderList []models.CzhHotelOrder
	if r := global.DB.Raw("SELECT * FROM czh_hotel_orders WHERE userid = ?", userid).Scan(&hotelOrderList); r.RowsAffected > 0 {
		return hotelOrderList, nil
	} else {
		return hotelOrderList, errors.New("没有查到数据")
	}
}

// 获取所有酒店信息;
func (h hotelDao) GetAllHotel() ([]models.CzhHotel, error) {
	var allHotel []models.CzhHotel
	if r := global.DB.Raw("SELECT * FROM czh_hotel").Scan(&allHotel); r.RowsAffected > 0 {
		return allHotel, nil
	} else {
		return allHotel, errors.New("没有查到数据")
	}
}

// 编辑酒店信息
func (h hotelDao) HotelEdit(id int, coverimg string, name string, score string, region string, phone string, address string) error {
	if r := global.DB.Exec("UPDATE czh_hotel SET name = ?, coverimg = ?, score = ?, region = ?, phone = ?, address = ? WHERE id = ?", name, coverimg, score, region, phone, address, id); r.Error != nil {
		return r.Error
	} else {
		return nil
	}
}

// 新增酒店信息
func (h hotelDao) HotelAdd(coverimg string, name string, score string, region string, phone string, address string) error {
	if r := global.DB.Exec("INSERT INTO czh_hotel (coverimg, name, score, region, phone, address) VALUES (?, ?, ?, ?, ?, ?)", coverimg, name, score, region, phone, address); r.Error != nil {
		return r.Error
	} else {
		return nil
	}
}

// 酒店删除
func (h hotelDao) HotelRemove(id int) error {
	if r := global.DB.Exec("DELETE FROM czh_hotel WHERE id = ?", id); r.Error != nil {
		return r.Error
	} else {
		return nil
	}
}
